package a.codetop;

import 其他.DataStructure.TreeNode;

import java.util.*;

public class _9Z型打印二叉树 {

    //z打印二叉树
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root==null) return res;

        Queue<TreeNode> que = new LinkedList<>();
        List<Integer> rotTmp  = new ArrayList<>(); rotTmp.add(root.val);
        que.add(root);

        int oldSize = que.size();
        int f=1;
        while(oldSize > 0){
            List<Integer> tmp = new ArrayList<>();
            for (int i = 0; i < oldSize; i++) {
                TreeNode poll = que.poll();
                tmp.add(poll.val);

                if(poll.left!=null){
                    que.add(poll.left);
                }
                if(poll.right!=null){
                    que.add(poll.right);
                }
            }
            oldSize = que.size();

            //res.add(tmp) 就是分层板子
            if(f==0){
                Collections.reverse(tmp);
                res.add(tmp); f=1;
            }else if(f==1){
                res.add(tmp); f=0;
            }
        }
        return res;
    }
}
